/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.buzzwords.utils;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Properties;
import org.apache.log4j.Logger;

public class MailClient extends Thread {

    Logger logger = Logger.getLogger(MailClient.class);
    final private String senderEmail = "com.buzzwords@gmail.com";
    final String senderPassword = "sz4Bj4vm9sA9";
    final String emailSMTPServer = "smtp.gmail.com";
    final String emailServerPort = "465";
    private String to = "";
    private String subject = "";
    private String messageBody = "";

    public MailClient(String to,
            String subject, String messageBody) {
        this.to = to;
        this.subject = subject;
        this.messageBody = messageBody;
    }

    private void sendMail() {
        // Setup mail server
        Properties props = System.getProperties();
        props.put("mail.smtp.user", senderEmail);
        props.put("mail.smtp.host", emailSMTPServer);
        props.put("mail.smtp.port", emailServerPort);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        // props.put("mail.smtp.debug", "true");
        props.put("mail.smtp.socketFactory.port", emailServerPort);
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        try {
            Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, auth);

            // Define a new mail message
            Message message = new MimeMessage(session);
            message.setContent(messageBody, "text/html");
            message.setSubject(subject);
            message.setFrom(new InternetAddress(senderEmail));
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
            

            // Send the message
            Transport.send(message);
        } catch (MessagingException me) {
            logger.fatal("Email failed", me);
        }
    }

    protected void addAtachments(String[] attachments, Multipart multipart)
            throws MessagingException, AddressException {
        for (int i = 0; i <= attachments.length - 1; i++) {
            String filename = attachments[i];
            MimeBodyPart attachmentBodyPart = new MimeBodyPart();

            //use a JAF FileDataSource as it does MIME type detection
            DataSource source = new FileDataSource(filename);
            attachmentBodyPart.setDataHandler(new DataHandler(source));

            //assume that the filename you want to send is the same as the
            //actual file name - could alter this to remove the file path
            attachmentBodyPart.setFileName(filename);

            //add the attachment
            multipart.addBodyPart(attachmentBodyPart);
        }
    }

    @Override
    public void run() {
        sendMail();
    }

    public String getMessageBody() {
        return messageBody;
    }

    public void setMessageBody(String messageBody) {
        this.messageBody = messageBody;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator {

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(senderEmail, senderPassword);
        }
    }
}
